<?php

declare (strict_types=1);
namespace App\Model;

use App\Helper\Utils;
use App\Traits\ModelLabelTrait;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property string $title 
 * @property int $category_id 
 * @property string $type 
 * @property string $author 
 * @property string $copyfrom 
 * @property string $thumb 
 * @property string $keywords 
 * @property string $description 
 * @property string $content 
 * @property string $published_at 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $deleted_at 
 */
class CmsPost extends Model
{
    use ModelLabelTrait,SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'cms_posts';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'category_id',
        'type',
        'author',
        'copyfrom',
        'thumb',
        'keywords',
        'description',
        'content',
        'published_at'
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'category_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function getThumbAttribute($field)
    {
        return $this->attributes[$field] = $field?config('common.image_prefix').$field:null;
    }

    public function setThumbAttribute($value)
    {
        return $this->attributes['thumb'] = str_replace(config('common.image_prefix'),'',$value);
    }

    public function getContentAttribute($field)
    {
        return $this->attributes[$field] = Utils::replaceContentImg($field);
    }

    public function setContentAttribute($value)
    {
        $value = str_replace('../','',$value);
        return $this->attributes['content'] = Utils::stripContentImgPrefix($value);
    }

    const TYPE_ARTICLE = 'article';
    const TYPE_HELP = 'help';
    const TYPE_NOTICE = 'notice';
    const TYPE_PAGE = 'page';

    const TYPE_LABELS = [
        self::TYPE_ARTICLE => '文章',
        self::TYPE_HELP => '帮助',
        self::TYPE_NOTICE => '通知',
        self::TYPE_PAGE => '单页'
    ];
}